package Producer_Consumer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-03-08
 * Time: 23:45
 */
public class Test_Producer_Consumer {
    public static ArrayBlockingQueue queue = new ArrayBlockingQueue(1000);
    private static void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(()->{
            for (int i = 1; i <= 1000; i++) {
                int n = i;
                try {
                    submit(()-> System.out.println("消费：" + n));
                    System.out.println("生产：" + i);
                    Thread.sleep(500);
                }catch (InterruptedException e){
                    throw new IllegalAccessError();
                }
            }
        });
        Thread thread2 = new Thread(()->{
            for (int i = 1; i <= 1000; i++) {
                try {
                    queue.take().run();
                    Thread.sleep(500);
                }catch (InterruptedException e){
                    throw new IllegalAccessError();
                }
            }
        });
        thread1.start();
        thread2.start();
    }
}
